package com.surfilter.msmp.web.es;

import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Description：同步业务数据至es
 * Author；JinHuatao
 * Date: 2019/9/26 13:59
 */
@Component
public class OptES {

    private Logger log = LoggerFactory.getLogger(OptES.class);

    @Autowired
    private RestHighLevelClient highLevelClient;

    //将数据插入es索引
    public void insertEs(String indexName, String jsonData){
        IndexRequest indexRequest = new IndexRequest(indexName).source(jsonData, XContentType.JSON);
        try {
            highLevelClient.index(indexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error("ES数据入库异常，{}", e);
        }
    }
}
